/*
 * Copyright 2007-2008  Lukasz Laszko lukaszlaszko@gmail.com
 * 
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and this
 * permission notice and warranty disclaimer appear in supporting
 * documentation, and that the name of the author not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * 
 * The author disclaim all warranties with regard to this
 * software, including all implied warranties of merchantability
 * and fitness.  In no event shall the author be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether
 * in an action of contract, negligence or other tortious action,
 * arising out of or in connection with the use or performance of
 * this software. 
 */
package org.pg.biomedics.asr.config;

import java.util.Properties;
import java.util.Enumeration;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

/**
 * Configuration manager responsinble for managing configuration loaded from 
 * custome properties file
 * @author Lukasz Laszko
 */
public class ConfigurationManager 
{
    // </editor-fold desc="Constants">
    
    private static final String WEBSERVICES_ENABLED_PROPERTY = "webservices.enabled";
    private static final String WEBSERVICES_WEBSERVER_CONFIGURATION_PROPERTY = "webservices.webserver.configuration";
    private static final String SCHEDULER_ENABLED_PROPERTY = "taskscheduler.enabled";
    private static final String SCHEDULER_MAX_THREADS_PROPERTY = "taskscheduler.maxthreads";
    private static final String SCHEDULER_VOICE_ENGINE_CONFIG_PROPERTY = "taskscheduler.voiceengineconfig";
    private static final String SERVICES_MEDIA_WORK_DIR_PROPERTY = "media.workdir";
    private static final String SERVICES_SQL_MAP_CONFIG_PROPERTY = "ibatis.sqlmapconfig";
    
    // </editor-fold>
    
    // <editor-fold desc="Properties">
    
    /**
     * Checks if web service is enabled in configuration
     * @return
     */    
    public boolean isWebServiceEnabled()
    {
        if (!System.getProperties().containsKey(WEBSERVICES_ENABLED_PROPERTY))
            return false;
        
        return System.getProperty(WEBSERVICES_ENABLED_PROPERTY).equals("true");
    }
    
    /**
     * Gets web server configuration file location
     * @return
     * @throws org.pg.biomedics.asr.config.ConfigurationException
     */
    public String getWebServerConfig() throws ConfigurationException
    {
        if (!System.getProperties().containsKey(WEBSERVICES_WEBSERVER_CONFIGURATION_PROPERTY))
            throw new ConfigurationException("Can't get web server config , configuration entry missing");
        
        return System.getProperty(WEBSERVICES_WEBSERVER_CONFIGURATION_PROPERTY);
    }
    
    /**
     * Checks if scheduler is enabled
     * @return 
     */
    public boolean isSchedulerEnabled()
    {
        if (!System.getProperties().containsKey(SCHEDULER_ENABLED_PROPERTY))
            return false;
        
        return System.getProperty(SCHEDULER_ENABLED_PROPERTY).equals("true");
    }
    
    /**
     * Gets scheduler max thread pools size 
     * @return max thread pools size
     * @throws org.pg.biomedics.asr.config.ConfigurationException
     */
    public int getSchedulerMaxThreads() throws ConfigurationException
    {
        if (!System.getProperties().containsKey(SCHEDULER_MAX_THREADS_PROPERTY))
            throw new ConfigurationException("Can't get scheduler threads pool size , configuration entry missing");
        
        return Integer.parseInt(System.getProperty(SCHEDULER_MAX_THREADS_PROPERTY));
    }
    
    /**
     * Gets voice engine configuration file location
     * @return
     * @throws org.pg.biomedics.asr.config.ConfigurationException
     */
    public String getVoiceEngineConfig() throws ConfigurationException
    {
        if (!System.getProperties().containsKey(SCHEDULER_VOICE_ENGINE_CONFIG_PROPERTY))
            throw new ConfigurationException("Can't get voice engine config location, configuration entry missing");
        
        return System.getProperty(SCHEDULER_VOICE_ENGINE_CONFIG_PROPERTY);
    }
    
    /**
     * Gets media work dir location
     * @return media work dir location
     * @throws org.pg.biomedics.asr.config.ConfigurationException
     */
    public String getMediaWorkDir() throws ConfigurationException
    {
        if (!System.getProperties().containsKey(SERVICES_MEDIA_WORK_DIR_PROPERTY))
            throw new ConfigurationException("Can't get media work dir, configuration entry missing");
        
        return System.getProperty(SERVICES_MEDIA_WORK_DIR_PROPERTY);
    }
    
    /**
     * Gets sql map file location
     * @return
     * @throws org.pg.biomedics.asr.config.ConfigurationException
     */
    public String getSqlMapConfig() throws ConfigurationException
    {
        if (!System.getProperties().containsKey(SERVICES_SQL_MAP_CONFIG_PROPERTY))
            throw new ConfigurationException("Can't get sql map file location, configuration entry missing");
        
        return System.getProperty(SERVICES_SQL_MAP_CONFIG_PROPERTY);
    }
    
    // </editor-fold>
    
    // <editor-fold desc="Methods">
    
    /**
     * Provides configuration load logic
     * @param propertiesFile custom properties file
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void configure(String propertiesFile) throws FileNotFoundException, IOException
    {
        Properties properties = new Properties();
            
        FileInputStream inStream = new FileInputStream(propertiesFile);
        properties.load(inStream);
        inStream.close();
            
        combineProperties(properties);
    }
    
    // </editor-fold>
    
    // <editor-fold desc="Support methods">
    
    private void combineProperties(Properties properties)
    {
        for (Enumeration<Object> keys = properties.keys(); keys.hasMoreElements(); )
        {
            Object key = keys.nextElement();
            
            if (!System.getProperties().containsKey(key))
                System.getProperties().put(key, properties.get(key));
        }
    }
    
    // </editor-fold>
}
